package com.sg.business.work.handler;

import java.util.Map;

import org.drools.runtime.process.ProcessInstance;
import org.drools.runtime.process.WorkflowProcessInstance;
import org.eclipse.core.commands.Command;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;
import com.sg.bpm.workflow.WorkflowService;
import com.sg.business.model.DummyModel;
import com.sg.business.model.Work;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.command.AbstractNavigatorHandler;
import com.sg.widgets.part.editor.DataObjectDialog;
import com.sg.widgets.viewer.ViewerControl;

public class ProcessVaribaleEditor extends AbstractNavigatorHandler {

	@Override
	protected void execute(PrimaryObject selected, IWorkbenchPart part,
			ViewerControl vc, Command command, Map<String, Object> parameters,
			IStructuredSelection selection) {
		if (selected instanceof Work) {
			try {
				DummyModel dy = ModelService
						.createModelObject(DummyModel.class);
				DataObjectDialog dialog = DataObjectDialog.openDialog(dy,
						"editor.allwork.processvaribale", true, null);
				int rc = dialog.getReturnCode();
				if (rc == DataObjectDialog.OK) {
					Work work = (Work) selected;
					String varibalename = (String) dy.getValue("varibalename");
					Object varibalevalue = dy.getValue("varibalevalue");
					ProcessInstance processInstance = work.getExecuteProcess();
					String processId = processInstance.getProcessId();
					long processInstanceId = work.getExecuteProcessId();
					WorkflowProcessInstance process = WorkflowService
							.getDefault().getProcessInstance(processId,
									processInstanceId);
					
					process.setVariable(varibalename, varibalevalue);
				}
			} catch (Exception e) {
				MessageUtil.showToast(e);
			}
		}

	}

}
